package itxx.itxx.servlet;

import itxx.itxx.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/ListStudnetServlet")
public class ListStudnetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过字符输入流读取到D:\\stu.txt文件中的数据
        BufferedReader br = new BufferedReader(new FileReader("D:\\stu.txt"));
        //将读取到的数据封装到Studnet对象中
        //创建集合
        ArrayList<Student> list = new ArrayList<>();
        //循环读取文件数据,并将多个对象存入到集合中
        String line;
        while((line=br.readLine())!=null){
            String[] split = line.split(",");
            Student student = new Student(split[0], Integer.parseInt(split[1]), Integer
                    .parseInt(split[2]));
            list.add(student);

        }
        br.close();
        //设置编码格式
        response.setContentType("text/htmnl;charset=UTF-8");
        //遍历集合,将数据响应到浏览器
        PrintWriter pw = response.getWriter();
      //  pw.write("学生姓名"+"学生年龄"+"学生成绩");
        for (Student student : list) {

            pw.write(student.getName()+","+student.getAge()+","+student.getSorce());
           pw.write("\r");
            pw.flush();
        }

    }
}
